<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\model;
use think\Db;
use think\Model;
use think\Cache;

class Daily extends Model
{
    /**
     * 获取日报列表
     * @param str $where <查询条件>
     * @param int $page <页码>
     * @param int $limit <每页显示条数>
     * @return array <日报列表信息>
     */
    public function getDailyData($where,$page,$limit)
    {
        $users = Cache::get('users');
        $count = Db::name('mob_daily_info')->where($where)->count();
        // 查询状态为1的用户数据 并且每页显示10条数据
        $dailyList = Db::name('mob_daily_info')->where($where)->limit(($page-1)*$limit,$limit)->order('createtime DESC')->select();
        foreach ($dailyList as &$val) {
            $val['createname'] = $users[$val['createname']]['cn_name'];
            $val['view_user'] = $users[$val['view_user']]['cn_name'];
            switch ($val['daily_type']) {
                case '1': $val['daily_type'] = '日报'; break;
                case '2': $val['daily_type'] = '周报'; break;
                case '3': $val['daily_type'] = '月报'; break;
                case '4': $val['daily_type'] = '年报'; break;
            }
        }
        $rowsList = count($dailyList) ? $dailyList : false;
        $arrData["code"] = 0;
        $arrData["msg"] = '';
        $arrData["count"] = $count;
        $arrData["data"] = $rowsList;
        return $arrData;
    }


    /**
     * 获取单个日报的信息
     * @param  str $id [日报ID]
     * @return arr     [日报信息]
     */
    public function getDailyInfo($id)
    {
        // $id = 27;
        $dailyInfo = Db::name('mob_daily_info')->where('id',$id)->find();
        return $dailyInfo;
    }


    /**
     * 获取日报评论
     * @param  [string] $daily_id [日报ID]
     * @return [arr]              [日报评论列表/空数组]
     */
    public function getDailyComment($daily_id)
    {
        $dailyCommentInfo = Db::name('mob_daily_comment')->where('daily_id',$daily_id)->select();
        $users = Cache::get('users');
        foreach ($dailyCommentInfo as &$val) {
            $val['createname'] = empty($users[$val['createname']]['cn_name'])? ' ':$users[$val['createname']]['cn_name'];
        }
        return $dailyCommentInfo;
    }


    /**
     * 删除日报
     * @param  str $id [日报id]
     * @return json    [删除状态]
     */
    public function delDaily($id)
    {
        $result = Db::name('mob_daily_info')->where('id',$id)->delete();
        if ($result){
            echo json_encode(array('success'=>true,'msg'=>'删除成功！'));
        } else {
            echo json_encode(array('msg'=>'删除失败！'));
        }
    }


}